package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.dto.ProductInputDTO;
import com.partners.dao.bo.PointProductDO;
import com.partners.dao.bo.ProductClassDO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.ProductService;
import com.partners.utils.ApiResultUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 积分产品管理
 * @author liangyongle
 * @date 2018-12-12
 */
@RestController
@RequestMapping("product")
public class ProductController {

    @Resource
    private ProductService productService;


    /**
     * 获取产品列表
     * @param paging Paging //分页数
     *        name String  //名字
     *        class_id Integer  //分类id
     *        isup Byte 是否上线
     * @return List
     */
    @PostMapping("/product_list")
    public ApiResult getProductList(@BeanValid @RequestBody Paging paging,
                                    @SimpleParam("title") String title,
                                    @SimpleParam("class_id") Integer classId,
                                    @SimpleParam("type") Integer type){
        Map<String,Object> list = productService.getProductList(paging,title,classId,type);
        return ApiResultUtil.ok(list);
    }

    /**
     * 获取产品详情
     * @param Id Integer
     * @return Object
     */
    @PostMapping("product_detail")
    public ApiResult getProductDetail(@SimpleParam("id") Integer Id){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Map<String,Object> list = productService.getProductDetail(Id);
        return ApiResultUtil.ok(list);
    }

    /**
     * 删除产品
     * @param Id Integer
     * @return Object
     */
    @PostMapping("product_delete")
    public ApiResult deleteProduct(@SimpleParam("id") Integer Id){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Integer result = productService.deleteProduct(Id);
        return ApiResultUtil.ok(result);
    }

    /**
     * 编辑产品
     * @param pointProductDO PointProductDO
     * @return Integer
     */
    @PostMapping("product_edits")
    public ApiResult editsProduct(@RequestBody PointProductDO pointProductDO){
        if(pointProductDO.getTitle() == null){
            throw BusinessException.of(ErrorMessageEnum.TITLE_NOT_FOUND);
        }
        if(pointProductDO.getClassId() == null){
            throw  BusinessException.of(ErrorMessageEnum.CLASS_NOT_FOUND);
        }
        if(pointProductDO.getPic() == null){
            throw  BusinessException.of(ErrorMessageEnum.PRICE_NOT_FOUND);
        }
        if(pointProductDO.getPic() == null){
            throw  BusinessException.of(ErrorMessageEnum.PIC_NOT_FOUND);
        }
        if(pointProductDO.getPoint() == null){
            throw  BusinessException.of(ErrorMessageEnum.POINT_NOT_FOUND);
        }
        if(pointProductDO.getStock() == null){
            throw  BusinessException.of(ErrorMessageEnum.STOCK_NOT_FOUND);
        }
        if(pointProductDO.getContent() == null){
            throw  BusinessException.of(ErrorMessageEnum.CONTENT_NOT_FOUND);
        }
        Integer result = productService.editProduct(pointProductDO);
        return ApiResultUtil.ok(result);
    }

    /**
     * 获取产品分类列表
     * @param
     * @return Integer
     */
    @PostMapping("edit_product")
    public ApiResult editProduct(@RequestBody ProductInputDTO productInputDTO){
        Integer result = productService.editsProduct(productInputDTO);
        return ApiResultUtil.ok(result);
    }

    /**
     * 获取产品分类列表
     * @param
     * @return Integer
     */
    @PostMapping("get_class_list")
    public ApiResult getClassList(@BeanValid @RequestBody Paging paging,
                                  @SimpleParam("name") String name){
        Map<String, Object> list = productService.getClassList(paging, name);
        return ApiResultUtil.ok(list);
    }



     /** 获取子分类列表
     * @param
     * @return Integer
     */
    @PostMapping("get_class_child")
    public ApiResult getClassChild(@SimpleParam("id") Integer Id){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        List<Map<String,Object>> result = productService.getProductClassChild(Id);
        return ApiResultUtil.ok(result);
    }

    /**
     * 获取分类详情
     * @param
     * @return Integer
     */
    @PostMapping("get_class_detail")
    public ApiResult getClassDetail(@SimpleParam("id") Integer Id){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        ProductClassDO result = productService.getProductClassDetail(Id);
        return ApiResultUtil.ok(result);
    }




    /**
     * 编辑分类
     * @param productClassDO ProductClassDO
     * @return Integer
     */
    @PostMapping("class_edit")
    public ApiResult editClass(@RequestBody ProductClassDO productClassDO){
        if(productClassDO.getName() == null){
            throw BusinessException.of(ErrorMessageEnum.NAME_NOT_FOUND);
        }

        Integer result = productService.editProductClass(productClassDO);
        return ApiResultUtil.ok(result);
    }


    /**
     * 删除分类
     * @param
     * @return Integer
     */
    @PostMapping("product_class_delete")
    public ApiResult deleteProductClass(@SimpleParam("id") Integer Id){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Integer result = productService.deleteProductClassDetail(Id);
        return ApiResultUtil.ok(result);
    }


    /**
     * 产品上下架
     * @param Id Integer up Integer
     * @return Integer
     */
    @PostMapping("product_up")
    public ApiResult productUp(@SimpleParam("id") Integer Id,@SimpleParam("up") Integer up){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        if(up == null){
            throw BusinessException.of(ErrorMessageEnum.UP_NOT_FOUND);
        }
        if(up >1 || up < 0){
            throw BusinessException.of(ErrorMessageEnum.UP_VALUE_ERROR);
        }
        productService.editProductUp(Id,up);
        return ApiResultUtil.ok(SuccessMessageEnum.EDIT_SUCCESS);
    }







}
